package org.yonggan.hystrix;

import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.yonggan.UserFeginClientWithFactory;
import org.yonggan.UserFeignClient;
import org.yonggan.entity.User;

@Component
public class HystrixClientFallbackFactory implements FallbackFactory<UserFeignClient> {

    private static final Logger LOGGER = LoggerFactory.getLogger(HystrixClientFallbackFactory.class);

    @Override
    public UserFeignClient create(Throwable cause) {
        // 打印异常信息
        LOGGER.info("fallback; reason was: {} " + cause.getMessage());
        return new UserFeginClientWithFactory() {
            @Override
            public User findById(Long id) {
                User user = new User();
                user.setId(-1L);
                return user;
            }
        };
    }
}
